<template>
  <div class="">
    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中..." border fit highlight-current-row>
      <el-table-column align="center" label="任务名称" prop="name" />
      <el-table-column align="center" label="上一次执行时间" prop="preExecTime"/>
      <el-table-column align="center" label="下一次执行时间" prop="nextExecTime"/>
      <el-table-column align="center" label="上一次执行结果" prop="preExecResult"/>
      <el-table-column align="center" label="执行次数" prop="count"/>
      <el-table-column align="center" label="执行周期" prop="period"/>
      <el-table-column align="center" label="状态" prop="enable">
        <template slot-scope="scope">
          <el-switch v-permission="['sysSetting:taskOff','sysSetting:taskOn']" v-model="scope.row.status" :active-value="1" :inactive-value="2" active-color="#13ce66" @change="changeStatus(scope.row.name,$event)"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-permission="['sysTask:tasks']" v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="initList"/>
  </div>
</template>

<script>
import { TaskService } from '@/api/SystemService'
import Pagination from '@/components/Pagination'
import Notify from '@/utils/notify'

export default {
  name: 'Platform',
  components: { Pagination },
  data() {
    return {
      fileList: [], isAdd: true, list: null, total: 0, listLoading: true,
      listQuery: { pageNo: 1, pageSize: 20 }
    }
  },
  created() {
    this.initList()
  },
  methods: {
    initList() {
      this.listLoading = true
      TaskService.listTask(this.listQuery).then(result => {
        this.list = result.data
        this.total = result.totalCount
        this.listLoading = false
      })
    },
    changeStatus(name, status) {
      if (status === 1) {
        TaskService.startTask({ name: name }).then(() => {
          Notify.success(this, '设置成功!')
          this.initList()
        })
      } else {
        TaskService.stopTask({ name: name }).then(() => {
          Notify.success(this, '设置成功!')
          this.initList()
        })
      }
    }
  }
}
</script>
